package java67_adv6;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class GetNextBirthday {
	/***
	 * 10：用户输入出生日期（月-日），计算距离他的下个生日还有几天（注意：2月29的处理）
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		System.out.println("qingshru input :");
		String bir = in.next();// m-d
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sf.setLenient(false);
		Calendar c = Calendar.getInstance();
		Calendar cb = Calendar.getInstance();
		String birthday = c.get(Calendar.YEAR) + "-" + bir + " 23:59:59";

		// 单独考虑2-29
		if (bir.equals("2-29") || bir.equals("02-29")) {
			int year = c.get(Calendar.YEAR);
			while (true) {
				try {
					birthday = year + "-" + bir + " 23:59:59";
					Date date = sf.parse(birthday);
					cb.setTime(date);
					if (cb.after(c)) {// 今年的2-29生日还没有到
						if (cb.get(Calendar.YEAR) == c.get(Calendar.YEAR)) {
							int days = cb.get(Calendar.DAY_OF_YEAR) - c.get(Calendar.DAY_OF_YEAR);
							System.out.println("还有" + days + "天过生日");
						} else {
							int days = c.getActualMaximum(Calendar.DAY_OF_YEAR) - c.get(Calendar.DAY_OF_YEAR)
									+ cb.get(Calendar.DAY_OF_YEAR)
									+ 365 * (cb.get(Calendar.YEAR) - c.get(Calendar.YEAR) - 1);
							System.out.println("还要漫长的" + days + "天");
						}
						return;
					} else {
						year++;
					}

				} catch (ParseException e) {
					// e.printStackTrace();
					year++;
				}
			}

		}

		try {

			Date date = sf.parse(birthday);
			cb.setTime(date);
			if (cb.after(c)) {// 今年的生日还没有到
				int days = cb.get(Calendar.DAY_OF_YEAR) - c.get(Calendar.DAY_OF_YEAR);
				System.out.println("还有" + days + "天过生日");
			} else {
				cb.add(Calendar.YEAR, 1);
				int days = c.getActualMaximum(Calendar.DAY_OF_YEAR) - c.get(Calendar.DAY_OF_YEAR)
						+ cb.get(Calendar.DAY_OF_YEAR);
				System.out.println("要到明年过生日,还有" + days + "天过生日");
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}

}
